package com.zaoke.web.service.push.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.notification.PushNotificationPayload;
import javapns.notification.PushedNotification;
import javapns.notification.PushedNotifications;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;

import com.zaoke.web.dao.token.TokenDao;
import com.zaoke.web.service.push.PushService;

public class PushServiceImpl implements PushService{
	
	private Log logger = LogFactory.getLog(getClass());
	
	private String keystore;
	
	private String password;
	
	private boolean production;
	
	private TokenDao tokenDao;

	public void push(String msg) throws JSONException, CommunicationException, KeystoreException{
		push(msg, tokenDao.getAllTokens());
	}

	public void push(String msg, String token) throws JSONException, CommunicationException, KeystoreException{
		Set<String> tokens = new HashSet<String>();
		tokens.add(token);
		push(msg, tokens);
	}
	
	protected void push(String msg, Set<String> token) throws JSONException, CommunicationException, KeystoreException{
		PushNotificationPayload payLoad = new PushNotificationPayload();
		payLoad.addAlert(msg);
		payLoad.addBadge(1);
		payLoad.addSound("default");
		logger.info("[SEND]" + payLoad);
		logger.info(msg);
		System.out.println(msg);
//		System.out.println(payLoad);
//		try {
//			System.out.println(new String(msg.getBytes("ISO-8859-1")));
//			System.out.println(new String(msg.getBytes("UTF8")));
//			logger.info(new String(msg.getBytes("ISO-8859-1")));
//			logger.info(new String(msg.getBytes("UTF8")));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		PushedNotifications notifications = Push.payload(payLoad, keystore, password, production, new ArrayList<String>(token));
//		Push.alert("testtestest", keystore, password, production, token);
		if(notifications != null){
			for(PushedNotification notification : notifications)
				if(notification.isSuccessful())
					logger.info("[SUCCESS] " + notification.getDevice().getToken());
				else
					logger.info("[FAIL] " + notification.getDevice().getToken() + " - " + notification);
		}
	}
	public static void main(String[] args) throws JSONException, CommunicationException, KeystoreException {
		PushServiceImpl ps = new PushServiceImpl();
		ps.keystore = "C:\\Users\\scofieldlin\\Desktop\\PushDev.p12";
		ps.password = "Tiancixiaer1110";
		ps.production = false;
		ps.push("汉字汉字token", "5f456c931ca2d25c2429855a55e2c6292cd2008f5e0f72e4f68a07c4e01e30f5");
	}

	public String getKeystore() {
		return keystore;
	}

	public void setKeystore(String keystore) {
		this.keystore = keystore;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isProduction() {
		return production;
	}

	public void setProduction(boolean production) {
		this.production = production;
	}

	public TokenDao getTokenDao() {
		return tokenDao;
	}

	public void setTokenDao(TokenDao tokenDao) {
		this.tokenDao = tokenDao;
	}
}
